package cn.com.jslh.boarpro.main.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.blankj.utilcode.util.SPUtils;
import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.main.contract.DuanNaiContract;
import cn.com.jslh.boarpro.main.model.DuanNaiModelImpl;
import cn.com.jslh.boarpro.tables.DuanNaiTable;
import cn.com.jslh.boarpro.util.GsonUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * Created by Administrator on 2017/10/18.
 */

public class DuanNaiPresenterImpl implements DuanNaiContract.Presenter {
    private DuanNaiContract.View view;
    private DuanNaiContract.Model model;
    private Context context;
    private SlimAdapter slimAdapter;

    public DuanNaiPresenterImpl(DuanNaiContract.View view, Context context) {
        this.view = view;
        this.context = context;
        view.setPresenter(this);
        model = new DuanNaiModelImpl(context);
    }

    @Override
    public void start() {
        //加载数据// 根据当前生产线加载数据
        ArrayList<Object> datas = DataBaseUtil.GetData(context, DuanNaiTable.class, " where lineID =  '" + SPUtils.getInstance().getString(BaseApplication.FLINEID) + "'  order by _id desc");
        slimAdapter.updateData(datas).notifyDataSetChanged();
    }

    @Override
    public void bindRV(RecyclerView recyclerView) {
        slimAdapter = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .registerDefault(R.layout.duan_nai_list_item, new SlimInjector() {
                    @Override
                    public void onInject(final Object data, IViewInjector injector) {
                        final DuanNaiTable duanNaiTable = (DuanNaiTable) data;
                        String isWillOut = "否";
                        if(duanNaiTable.getIsWillOut().equals("1")){
                            isWillOut= "是";
                        }
                        injector.text(R.id.duannaibianhao, (slimAdapter.getData().size() - slimAdapter.getData().indexOf(data)) + "")
                                .text(R.id.muzhu, duanNaiTable.getMuZhuErHao())
                                .text(R.id.pinzhong, duanNaiTable.getPinZhong())
                                .text(R.id.nowZhuShe, duanNaiTable.getDqZhuShe())
                                .text(R.id.nowZhuQun, duanNaiTable.getDqPigFlk())
                                .text(R.id.goZhuShe, duanNaiTable.getGoZhuShe())
                                .text(R.id.goZhuQun, duanNaiTable.getGoZPigFlk())
                                .text(R.id.isWillOut,isWillOut)
                                .clicked(R.id.delete, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        DataBaseUtil.excute(context, "delete from DuanNaiTable where muZhuErHao ='" + duanNaiTable.getMuZhuErHao() + "'");
                                        slimAdapter.getData().remove(data);
                                        slimAdapter.notifyDataSetChanged();
                                    }
                                });
                    }
                }).enableEmptyView(context, R.layout.empty_layout).attachTo(recyclerView);
    }

    @Override
    public void upAction() {
        ArrayList<Object> arrayList = DataBaseUtil.GetData(context, DuanNaiTable.class, " where lineID = '" + SPUtils.getInstance().getString(BaseApplication.FLINEID) + "'");
        if (arrayList.size() == 0) {
            Toast.makeText(context, "当前无记录待上传", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userID", SPUtils.getInstance().getString(BaseApplication.FUSERID));
            jsonObject.put("entry", new JSONArray(GsonUtil.toJson(arrayList)));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        model.upAction(jsonObject.toString(), new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                if (s.contains("成功")) {
                    DataBaseUtil.excute(context,"delete from DuanNaiTable where lineID = '"+SPUtils.getInstance().getString(BaseApplication.FLINEID)+"'");
                    start();
                }
                Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
